package zdream.rockchronicle.core.graphics;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;

import java.util.List;
import java.util.Objects;

import zdream.control.graphics.IPainter;
import zdream.control.world.MFoe;
import zdream.control.world.Ticker;
import zdream.rockchronicle.RockChronicle;

import static zdream.control.world.MFoe.*;

/**
 * debug 用的 Painter
 * 
 * @author Zdream
 * @since 0.0.1
 * @date
 *   2019-05-09 (created)
 */
public class ShapePainter implements IPainter {
	public final RockChronicle app;
	private final ShapeRenderer renderer;
	private final SpriteBatch batch;

	public ShapePainter(RockChronicle app) {
		this.app = Objects.requireNonNull(app);
		renderer = new ShapeRenderer();
		batch = app.graphics.batch;
	}

	@Override
	public boolean isEnable() {
		return app.config.debug;
	}

	private Color defaultColor(MFoe foe) {
		switch (foe.type) {
			case TYPE_LEADER: return Color.WHITE;
			case TYPE_FOE: return Color.ORANGE;
			case TYPE_ELITE: return Color.YELLOW;
			case TYPE_BULLET: return Color.CYAN;
			case TYPE_TRAP: return Color.RED;
			case TYPE_MASS: return Color.GREEN;
			case TYPE_FIELD: return Color.DARK_GRAY;
			default: return Color.GRAY;
		}
	}

	@Override
	public void draw(Ticker ticker) {
		renderer.setProjectionMatrix(batch.getProjectionMatrix());
		final List<MFoe> foes = app.levels.foes;
		for (MFoe foe : foes) {
			drawBox(foe);
		}
	}

	private void drawBox(MFoe box) {
		renderer.begin(ShapeType.Line);
		renderer.setColor(defaultColor(box));

		box.flush();
		float xLeft = box.pos.x;
		float xRight = xLeft + box.pos.width;
		float yBottom = box.pos.y;
		float yTop = yBottom + box.pos.height;

		renderer.line(xLeft, yBottom, xRight, yBottom);
		renderer.line(xRight, yBottom, xRight, yTop);
		renderer.line(xRight, yTop, xLeft, yTop);
		renderer.line(xLeft, yTop, xLeft, yBottom);
		renderer.end();
	}

	@Override
	public int zIndex() {
		return 9999;
	}

	@Override
	public void dispose() {
		renderer.dispose();
	}
}
